<template>
  <el-dialog
    :title="!dataForm.pid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="pname">
      <el-input v-model="dataForm.pname" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="工堆位逻辑名" prop="position">
      <el-input v-model="dataForm.position" placeholder="工堆位逻辑名"></el-input>
    </el-form-item>
    <el-form-item label="工堆位描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="工堆位描述"></el-input>
    </el-form-item>
    <el-form-item label="X" prop="coordx">
      <el-input v-model="dataForm.coordx" placeholder="X"></el-input>
    </el-form-item>
    <el-form-item label="Y" prop="coordy">
      <el-input v-model="dataForm.coordy" placeholder="Y"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          pid: 0,
          pname: '',
          position: '',
          description: '',
          coordx: '',
          coordy: '',
          coordz: ''
        },
        dataRule: {
          pname: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          position: [
            { required: true, message: '工堆位逻辑名不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '工堆位描述不能为空', trigger: 'blur' }
          ],
          coordx: [
            { required: true, message: 'X不能为空', trigger: 'blur' }
          ],
          coordy: [
            { required: true, message: 'Y不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.pid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.pid) {
            this.$http({
              url: this.$http.adornUrl(`/pos/pos/info/${this.dataForm.pid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pname = data.pos.pname
                this.dataForm.position = data.pos.position
                this.dataForm.description = data.pos.description
                this.dataForm.coordx = data.pos.coordx
                this.dataForm.coordy = data.pos.coordy
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/pos/pos/${!this.dataForm.pid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'pid': this.dataForm.pid || undefined,
                'pname': this.dataForm.pname,
                'position': this.dataForm.position,
                'description': this.dataForm.description,
                'coordx': this.dataForm.coordx,
                'coordy': this.dataForm.coordy,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
